/**
 * Created by bian on 16-3-9.
 */
var render = require('../../instances/render.js');
var auth = require('../../helpers/auth.js');
var db = require('../../models/index.js');
var debug = require('../../instances/debug');
var sequelize = require('sequelize');
var util = require('util');

module.exports = (router) => {
    router.get('/adminer-super/shops-ctrl',function *(){
        this.body = yield render('admin/shops-ctrl.html',{});
    });

    router.get('/adminer-super/shops-ctrl/data',function *(){
        var user = yield auth.user(this);
        if(user.type === 100){
            this.body = yield db.models.shops.findAll({
                attributes:['id','title','mainImg'],
                include:[{
                    model:db.models.Adminer,
                    attributes:['id']
                }]
            });
        }
    });

    router.post('/adminer-super/shops-ctrl/del',function *(){
        this.checkParams('id').notEmpty().isInt().toInt();
        var user = yield auth.user(this);
        var AdminerNum = yield db.models.shops.findOne({
            attributes:['id'],
            where:{
                id:this.request.body.id
            },
            include:[{
                model:db.models.Adminer,
                attributes:['id']
            }]
        });
        if(user.type === 100 && !AdminerNum.Adminers.length){
            yield db.models.shops.update({
               status: -1
            },{
                where:{
                    id:this.request.body.id
                }
            });

            //购物车清理
            var GoodsData = yield db.models.Goods.findAll({
                where:{
                    shopId:this.request.body.id
                }
            });
            for(var x in GoodsData){
                yield db.models.ShoppingCart.destroy({
                    where:{
                        GoodId:GoodsData[x].id
                    }
                });
            }
            this.body = {
                status: true
            };
        }
    });

    //商店编辑与添加和shop-info合并，只不过多了个id

};